/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.oimwrapper.testers.models;

import com.thortech.util.logging.Logger;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;

/**
 *
 */
public class JDBCHandler {

    private Connection conn;
    private static final Logger logger = Logger.getLogger(JDBCHandler.class.getName());

    public JDBCHandler() throws Exception {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
            String url = "jdbc:oracle:thin:@192.168.56.102:1521:ORCL";
            conn = DriverManager.getConnection(url, "oim_custom", "abcd1234");
        } catch (ClassNotFoundException ex) {
            logger.error(ex.getMessage(),ex);
            throw ex;
        } catch (IllegalAccessException ex) {
            logger.error(ex.getMessage(),ex);
            throw ex;
        } catch (InstantiationException ex) {
            logger.error(ex.getMessage(),ex);
            throw ex;
        } catch (SQLException ex) {
            logger.error(ex.getMessage(),ex);
            throw ex;
        }
    }

    @Test
    public void mainTest()
    {
        logger.debug("HERE");
        List<OIMField> fields = getOimFieldMaps("Resource C","CREATEUSER");
        
        for(OIMField field : fields)
        {
            logger.debug("" + field);
        }
    }
    
    
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        close();
    }

    public void close() {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException sqle) {
        }
    }

    public List<OIMField> getOimFieldMaps(String resourceName, String transType) {

        List fieldList = new ArrayList();

        String query = "SELECT * FROM OIM_ARM_MAPPING WHERE RESOURCE_NAME=? AND REQUEST_TYPE=?";
        try {
            PreparedStatement ps = conn.prepareStatement(query);
            ps.setString(1, resourceName);
            ps.setString(2,transType);
            ResultSet rs = ps.executeQuery();
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();
            
            while (rs.next()) {
                HashMap row = new HashMap(columns);
                for(int i=1; i<=columns; ++i){
                    String beanColName = getBeanColumnName(md.getColumnName(i));
                    //logger.debug("BeanCol:" + beanColName);
                    row.put(beanColName,rs.getObject(i));
                }
                OIMField oimField = new OIMField();
                BeanUtils.populate(oimField, row);
                fieldList.add(oimField);
                
            }
        } catch (SQLException ex) {
            logger.error(ex.getMessage());
        } catch(IllegalAccessException ex)
        {
            logger.error(ex.getMessage());
        }
        catch(InvocationTargetException ex)
        {
            logger.error(ex.getMessage());
        }  
        //fieldList.add(new OIMField());
        return fieldList;
    }
    
    private String getBeanColumnName(String dbName) {
        String[] words = dbName.split("_");
        String string = "";
        for (String w : words) //This line is an easy way to capitalize a word
        {
            string += w.toUpperCase().replace(w.substring(1), w.substring(1).toLowerCase());
        }
        string = string.substring(0,1).toLowerCase() + string.substring(1);
        return string;
    }
}
